package com.black.platform.user.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.black.common.annotation.DataScope;
import com.black.platform.user.po.ShareUser;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * @author linwenfeng
 */
@Mapper
@DataScope(scopeType = "STAFF", tableAlias = "su")
public interface ShareUserMapper extends BaseMapper<ShareUser> {
    /**
     * 获取权限下的所有用户
     * @param comCode 归属机构
     * @param permissionCode 权限代码
     * @return r
     */
    @Select("<script>" +
            "SELECT su.* FROM share_user su,user_position up,position_role pr,role_permission rp,permission p " +
            "WHERE su.id = up.user_id AND up.position_id = pr.position_id AND pr.role_id =rp.role_id AND rp.permission_id = p.id " +
            "AND p.permission_code =#{ permissionCode }" +
            "<when test='comCode!=null'>" +
            "AND su.com_code like #{comCode} " +
            "</when>" +
            "</script>")
    List<ShareUser> findPowerUser(@Param("comCode") String comCode, @Param("permissionCode") String permissionCode);

    /**
     * 用户搜索
     * @param page 分页
     * @param valid 是否保含失效
     * @param name 用户名
     * @param serialNo 人工序号
     * @param comCodeList 权限机构
     * @return r
     */
    @Select("<script>" +
            "SELECT su.*,group_concat(p2.position_name order by up.orders )positionName " +
            "FROM share_user su " +
            "left join user_position up on up.user_id = su.id " +
            "left join position p2 on p2.id = up.position_id " +
            "<where>" +
            "<when test='valid!=null and valid!=\"\"'>" +
            "AND su.valid = #{valid} " +
            "</when>" +
            "<when test='name!=null and name!=\"\"'>" +
            "AND su.name = #{name} " +
            "</when>" +
            "<when test='serialNo!=null and serialNo!=\"\"'>" +
            "AND su.serial_no = #{serialNo} " +
            "</when>" +
            "<when test='platform!=null '>" +
            "AND su.platform = #{platform} " +
            "</when>" +
            "<when test='comCodeList!=null'>" +
            "AND su.com_code in " +
            "<foreach item='item' collection='comCodeList' separator=',' open='(' close=')'>" +
            "#{item}" +
            "</foreach>" +
            "</when>" +
            "</where>" +
            "group by su.id " +
            "</script>")
    IPage<ShareUser> search(Page<ShareUser> page, @Param("valid") Integer valid,
                            @Param("name") String name, @Param("serialNo") String serialNo,@Param("platform")Integer platform, @Param("comCodeList") List<String> comCodeList);

    @Update("<script>" +
            "UPDATE share_user set iconUrl=#{path} WHERE id=#{userId}" +
            "</script>")
    int updateIcon(@Param("userId") Long userId, @Param("path") String path);
}
